$(function() {
	var paramTable;
	var data = $("#tabContent > .tab-pane.active").data();
	var depGroupCode = data.depGroupCode;
	var depGroupDesc = data.depGroupDesc;
	var dateFrom = data.dateFrom;
	var dateTo = data.dateTo;
	var param = data.param;
	var forecastYear = data.forecastYear;
	var AvgDaysL = 0;
	var AvgDaysH = 0;
	var AvgFeeL = 0;
	var AvgFeeH = 0;
	var AvgDrugFeeL = 0;
	var AvgDrugFeeH = 0;
	var AvgDrugMateFeeL = 0;
	var AvgDrugMateFeeH = 0;
	$('#' + tabId + ' #date-from-dep').html(dateFrom);
	$('#' + tabId + ' #date-to-dep').html(dateTo);
	//全院预算数据
	$.post(contextPath + "/DrgsIndexCal/indexCal.dhc", {
		'pardepgroupCode' : depGroupCode,
		'dateFrom' : dateFrom,
		'dateTo' : dateTo,
		'quotaCode' : "ForeCast",
		'monitorType' : "hosp",
		'msCode' : "std_computer",
		'hospCode':paramTabPage[tabId].hospCode,
		'compareType' : "std"
	}).done(function(result) {
		//科室组预算数据及标杆
		$.post(contextPath + "/DrgsIndexCal/indexCal.dhc", {
			'pardepgroupCode' : depGroupCode,
			'dateFrom' : dateFrom,
			'dateTo' : dateTo,
			'quotaCode' : "ForeCast",
			'monitorType' : "dep",
			'compareType' : "std",
			'hospCode':paramTabPage[tabId].hospCode,
			'msCode' : "std_computer"
		}).done(function(result_1) {
			var data = [];
			var hospData = {};
			if (result.data && result.data.length > 0) {
				hospData = result.data[0];
				data.push(hospData);
			}
			if (result_1.data && result_1.data.length > 0) {
				data = data.concat(result_1.data);
			}
			var columns = [ {
				"data" : 'ddesc1',
				"render" : function(obj, type, full) {
					var ddesc = full.ddesc;
					if (ddesc == "全院") {
						return depGroupDesc;
					}
					return ddesc;
				}
			}, {
				"data" : 'TotalCaseCnt'
			}, {
				"data" : 'AvgDays'
			}, {
				"data" : 'AvgDayscc'
			}, {
				"data" : 'AvgFee'
			}, {
				"data" : 'AvgFeecc'
			}, {
				"data" : 'AvgDrugFee'
			}, {
				"data" : 'AvgDrugFeecc'
			}, {
				"data" : 'AvgDrugMateFee'
			}, {
				"data" : 'AvgDrugMateFeecc'
			} ];

			var paramDatatable = {
				lengthChange : false,
				info : false,
				searching : false,
				order : [ [ 1, "desc" ] ]
			};
			$("#" + tabId + " #foredatadep-table").dataTableBasic(columns, data, paramDatatable);
		});
	});
	var array = param.split("-");
	for (var i = 0; i < array.length; i++) {
		var as = array[i].split(":");
		if (as[0] == "AvgDays") {
			AvgDaysL = as[1];
			AvgDaysH = as[2];
		}
		if (as[0] == "AvgFee") {
			AvgFeeL = as[1];
			AvgFeeH = as[2];
		}
		if (as[0] == "AvgDrugFee") {
			AvgDrugFeeL = as[1];
			AvgDrugFeeH = as[2];
		}
		if (as[0] == "AvgDrugMateFee") {
			AvgDrugMateFeeL = as[1];
			AvgDrugMateFeeH = as[2];
		}
		if(i==(array.length-1)){
			$.post(contextPath + "/ForeCast/loadDepParam.dhc", {
				pardepgroupCode : depGroupCode,
				forecastYear : forecastYear,
				dateFrom : dateFrom,
				dateTo : dateTo
			}).done(function(result) {
				var columns = [ {
					"data" : 'depDesc1',
					"render" : function(obj, type, full) {
						return "<input type='checkbox' />"+full.depDesc;
					}
				}, {
					"data" : 'TotalCaseCnt'
				}, {
					"data" : "AvgDaysL1",
					"render" : function(obj, type, full) {
						var avgDaysL =full.AvgDaysL?full.AvgDaysL:AvgDaysL;
						return "<input id='" + full.depCode + "_AvgDaysL' type='text' style='width:50px' value='" + avgDaysL + "'></input>";
					}
				}, {
					"data" : "AvgDaysHo",
					"render" : function(obj, type, full) {
						var avgDaysH = full.AvgDaysH ? full.AvgDaysH : AvgDaysH;
						return "<input id='" + full.depCode + "_AvgDaysH' type='text' style='width:50px' value='" + avgDaysH + "'></input>";
					}
				}, {
					"data" : "AvgFeeLo",
					"render" : function(obj, type, full) {
						var avgFeeL = full.AvgFeeL ? full.AvgFeeL : AvgFeeL;
						return "<input id='" + full.depCode + "_AvgFeeL' type='text' style='width:50px' value='" + avgFeeL + "'></input>";
					}
				}, {
					"data" : "AvgFeeHo",
					"render" : function(obj, type, full) {
						var avgFeeH = full.AvgFeeH ? full.AvgFeeH : AvgFeeH;
						return "<input id='" + full.depCode + "_AvgFeeH' type='text' style='width:50px' value='" + avgFeeH + "'></input>";
					}
				}, {
					"data" : "AvgDrugFeeLo",
					"render" : function(obj, type, full) {
						var avgDrugFeeL = full.AvgDrugFeeL ? full.AvgDrugFeeL : AvgDrugFeeL;
						return "<input id='" + full.depCode + "_DrugFeeL' type='text' style='width:50px' value='" + avgDrugFeeL + "'></input>";
					}
				}, {
					"data" : "AvgDrugFeeHo",
					"render" : function(obj, type, full) {
						var avgDrugFeeH = full.AvgDrugFeeH ? full.AvgDrugFeeH : AvgDrugFeeH;
						return "<input id='" + full.depCode + "_DrugFeeH' type='text' style='width:50px' value='" + avgDrugFeeH + "'></input>";
					}
				}, {
					"data" : "AvgDrugMateFeeLo",
					"render" : function(obj, type, full) {
						var avgDrugMateFeeL = full.AvgDrugMateFeeL ? full.AvgDrugMateFeeL : AvgDrugMateFeeL;
						return "<input id='" + full.depCode + "_AvgDrugMateFeeL' type='text' style='width:50px' value='" + avgDrugMateFeeL + "'></input>";
					}
				}, {
					"data" : "AvgDrugMateFeeHo",
					"render" : function(obj, type, full) {
						var avgDrugMateFeeH = full.AvgDrugMateFeeH ? full.AvgDrugMateFeeH : AvgDrugMateFeeH;
						return "<input id='" + full.depCode + "_AvgDrugMateFeeH' type='text' style='width:50px' value='" + avgDrugMateFeeH + "'></input>";
					}
				} ];
				var paramDatatable = {
					lengthChange : false,
					info : false,
					searching : false,
					order : [ [ 1, "desc" ] ],
					paging : false,
					scrollX : false,
					scrollY : 400
				};
				var data = [];
				if (result.success && result.data && result.data.length > 0) {
					data = result.data;
				}
				paramTable = $("#" + tabId + " #paramDep-table").dataTableBasic(columns,data, paramDatatable);
			});
		}
	}
	/*编辑修改批量*/
	$("#" + tabId + " #mergeParamOk").click(function() {
				$("#" + tabId + " #paramDep-table tbody").find("tr").each(
						function(i, n) {
							if ($("#" + tabId + " #paramQuotaCheck").val() == "AvgDays") {
								if ($("#" + tabId + " input[name='checkAll']:checked").val() == 1
										|| ($("#" + tabId + " input[name='checkAll']:checked").val() == 0 && $(n).find("td").eq(0).find("input[type='checkbox']").is(':checked'))) {
									$(n).find("td").eq(2).find("input").attr("value", $("#" + tabId + " #overManageID").val());
									$(n).find("td").eq(3).find("input").attr("value", $("#" + tabId + " #notOverManageID").val());
								}
							}
							if ($("#" + tabId + " #paramQuotaCheck").val() == "AvgFee") {
								if ($("#" + tabId + " input[name='checkAll']:checked").val() == 1
										|| ($("#" + tabId + " input[name='checkAll']:checked").val() == 0 && $(n).find("td").eq(0).find("input[type='checkbox']").is(':checked'))) {
									$(n).find("td").eq(4).find("input").attr("value", $("#" + tabId + " #overManageID").val());
									$(n).find("td").eq(5).find("input").attr("value", $("#" + tabId + " #notOverManageID").val());
								}
							}
							if ($("#" + tabId + " #paramQuotaCheck").val() == "AvgDrugFee") {
								if ($("#" + tabId + " input[name='checkAll']:checked").val() == 1
										|| ($("#" + tabId + " input[name='checkAll']:checked").val() == 0 && $(n).find("td").eq(0).find("input[type='checkbox']").is(':checked'))) {
									$(n).find("td").eq(6).find("input").attr("value", $("#" + tabId + " #overManageID").val());
									$(n).find("td").eq(7).find("input").attr("value", $("#" + tabId + " #notOverManageID").val());
								}
							}

							if ($("#" + tabId + " #paramQuotaCheck").val() == "AvgDrugMateFee") {
								if ($("#" + tabId + " input[name='checkAll']:checked").val() == 1
										|| ($("#" + tabId + " input[name='checkAll']:checked").val() == 0 && $(n).find("td").eq(0).find("input[type='checkbox']").is(':checked'))) {
									$(n).find("td").eq(8).find("input").attr("value", $("#" + tabId + " #overManageID").val());
									$(n).find("td").eq(9).find("input").attr("value", $("#" + tabId + " #notOverManageID").val());
								}
							}
						})
				drgUI.success("修改成功");
			});
	$("#" + tabId + " #depParamSave_btn").click(function() {
		var str = "";
		var rows = paramTable.columns().context[0].aoData;
		$.each(rows, function(i, n) {
			n = n.anCells;
			var depCode = $(n[2]).find("input").attr("id") ? $(n[2]).find("input").attr("id").split("_")[0] : "";
			str += depCode + "|";
			var avgDaysL = $(n[2]).find("input").val();
			var avgDaysH = $(n[3]).find("input").val();
			str += "AvgDays:" + avgDaysL + ":" + avgDaysH;
			str += "-";
			var avgFeeL = $(n[4]).find("input").val();
			var avgFeeH = $(n[5]).find("input").val();
			str += "AvgFee:" + avgFeeL + ":" + avgFeeH;
			str += "-";
			var drugFeeL = $(n[6]).find("input").val();
			var drugFeeH = $(n[7]).find("input").val();
			str += "AvgDrugFee:" + drugFeeL + ":" + drugFeeH;
			str += "-";
			var drugMateFeeL = $(n[8]).find("input").val();
			var drugMateFeeH = $(n[9]).find("input").val();
			str += "AvgDrugMateFee:" + drugMateFeeL + ":" + drugMateFeeH;
			str += ",";
		})
		str = str.substr(0, str.length - 1)
		$.post(contextPath + "/ForeCast/saveParamDepData.dhc", {
			adjustParameter : str,
			forecastYear : forecastYear,
			dateFrom : dateFrom,
			dateTo : dateTo
		}).done(function(result) {
			if (result.success) {
				drgUI.success("保存完毕");
			}
		});
	});
});